
export function formValidation(inputSelect,regex,error_message,messageBoxSelect,success_message = '') {
    let status = {ok:false,message:error_message,value:'',target:$(inputSelect)}
    // 失去焦点，检查
    status.target.blur(function() {
        let text = $(inputSelect).val()
        let messageBox = $(messageBoxSelect);
        if ( ! regex.test(text)) {
            status.ok = false
            messageBox.html(status.message = error_message)
            messageBox.css({
                color: "red"
            })
        }else {
            status.ok = true
            messageBox.html(status.message = success_message)
            messageBox.css({
                color: "#499c54"
            })
        }
        status.value = text
    });
    return status;
}
export function formVerify(...formItems) {
    return function () {
        for(let formItem of formItems) {
            if (! formItem.ok) return false;
        }
        return true;
    }
}
export function sendCodeWait(sendButtonSelect,waitSecondCount) {
    let activeWaitSecondCount = waitSecondCount;
    let sendButton = $(sendButtonSelect);
    let originalButtonName = sendButton.text();
    sendButton.attr("disabled",true)
    function refreshWaitStatus() {
        sendButton.html(activeWaitSecondCount+"秒后可重发")
    }
    refreshWaitStatus()
    let timer = null;
    timer = setInterval(()=>{
        activeWaitSecondCount--
        refreshWaitStatus()
        if (activeWaitSecondCount <= 0) {
            clearInterval(timer)
            // 还原为原来的
            sendButton.text(originalButtonName)
            sendButton.attr("disabled",false)
        }
    },1000)

}
